package org.gldap;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Hashtable;
import java.util.LinkedList;

import org.apache.log4j.Logger;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;

public class Parser {


	private static Logger log4j = Logger.getLogger(Parser.class);

	LinkedList<LinkedList> parseContacts( Document _dom){
		log4j.warn("parsing the contacts");
		log4j.warn("xml version: " +_dom.getXmlVersion());
		log4j.warn("xml encoding: " + _dom.getXmlEncoding());
		NodeList entries = _dom.getElementsByTagName("entry");
		
		// loop in all contacts
		LinkedList <LinkedList> allContacts = new LinkedList();
		for (int i = 0 ; i < entries.getLength(); i++){
			
			LinkedList <Hashtable> ll = new LinkedList();
			
			/*System.out.println("node name: " + entries.item(i).getNodeName());
			System.out.println("node type: " + entries.item(i).getNodeType());
			System.out.println("parent # attributes: " + entries.item(i).getAttributes().getLength());
			System.out.println("parent # children: " + entries.item(i).getChildNodes().getLength());
			  	
			//contacts[i] = new Contact(entries.item(i).getAttributes().getNamedItem("title").toString());
			System.out.println("hey");
			System.out.println(entries.item(i).getNodeName());
			*/
			NodeList contactNode = entries.item(i).getChildNodes();
				// attributes of the contact
				for (int e = 0; e < contactNode.getLength(); e++){
					NamedNodeMap childrenAttributes = contactNode.item(e).getAttributes();
					Hashtable<String,String> th = new Hashtable<String, String>();
					for ( int a = 0; a < childrenAttributes.getLength() ; a++){
						th.put(childrenAttributes.item(a).getNodeName() ,childrenAttributes.item(a).getNodeValue());
						
					}
					th.put("tagName", contactNode.item(e).getNodeName() );
					if ( contactNode.item(e).hasChildNodes() ){
						th.put("nodeValue", contactNode.item(e).getChildNodes().item(0).getTextContent());
					} else {
						try {
							th.put("nodeValue", contactNode.item(e).getNodeValue() );
						}catch(Exception exc) {
						
						}
					}
					ll.add(th);
					//System.out.println(th.toString());
				}
				allContacts.add(ll);
		}
		return allContacts;
	}

	public Document parseTextIntoDomDocument(String _xml) {
		DOMParser parser = new DOMParser();
		try {
			parser.parse(new InputSource(new java.io.StringReader(_xml)));
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return parser.getDocument();
	}

	public String readTextFile(String _filename) {
		String s = "";
		try {
			BufferedReader in = new BufferedReader(new FileReader(_filename));
			String str;
			while ((str = in.readLine()) != null) {
				s = s + str;
			}
			in.close();
		} catch (IOException e) {
		}
		return (s);
	}

}




